package com.yeyaomai.dksns.flowis.quartz;

import java.text.ParseException;
import java.util.Date;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.data.mongodb.core.query.Criteria;
import com.yeyaomai.dksns.flowis.dao.DetailDao;
import com.yeyaomai.dksns.flowis.dao.MongoDBBasicDao;
import com.yeyaomai.dksns.flowis.dao.TodayDao;
import com.yeyaomai.dksns.flowis.domain.Today;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisStatDetailDTO;
import com.yeyaomai.dksns.flowis.util.CollectionNames;
import com.yeyaomai.dksns.flowis.util.Globals;

public class TodayExecutor extends MongoDBBasicDao<Today> {
	@Resource
	private DetailDao detailDao;
	
	@Resource
	private TodayDao todayDao;
	
	final Logger logger = Logger.getLogger("quartz");
	
	/**
	 * 执行添加操作
	 * @throws ParseException
	 */
	public void saveToday() throws ParseException {
		Date date = new Date();
		Date dates = new Date(date.getTime() - 1 * 3600 * 1000);
		String formDate = Globals.FORMAT_TYPE_A.format(dates);
		String toDate = Globals.FORMAT_TYPE_A.format(date);
		if(formDate.equals(toDate))
		{
			toDate = Globals.FORMAT_TYPE_A.format(new Date(Globals.FORMAT_TYPE_A.parse(toDate).getTime()+1 * 24 * 3600 * 1000));
		}
		todayDao.delToday();
		
			
			Today today = new Today();
			today.setDkId(1);
			Integer uniqueCount = this.getUVByTime(formDate, toDate);
			today.setUniqueHost(uniqueCount);
			Integer ip = this.getIpByTime(formDate, toDate);
			today.setIp(ip);
			Integer newuniqueHost = this.getNewUVByTime(formDate, toDate);
			today.setNewUniqueHost(newuniqueHost);
			todayDao.addToday(today);
		
		logger.info(date+"running saveReferDomain Task add Data times of the ");
		logger.info("#############################来路域名定时任务执行添加完毕######################");
	}

	/**
	 * 定时任务处理UV
	 */
	private int getUVByTime(String formDate, String toDate) {
		String mapFunction = "{ visitCount: 0}";
		String reduceFunction = "function(doc, prev) {prev.visitCount+=1}";
		Criteria criteria = Criteria.where("access_date").gte(formDate).lt(toDate);
		int uvcount = detailDao.group(criteria, CollectionNames.COOKIEID, mapFunction, reduceFunction, FlowisStatDetailDTO.class).getKeys();
		return uvcount;
	}

	/**
	 * 定时任务处理IP
	 */
	private int getIpByTime(String formDate, String toDate) {
		String mapFunction = "{ip: 0}";
		String reduceFunction = "function(doc, prev) {prev.ip+=1}";
		Criteria criteria = Criteria.where("access_date").gte(formDate).lt(toDate);
		int ipcount = detailDao.group(criteria, CollectionNames.IP, mapFunction, reduceFunction, FlowisStatDetailDTO.class).getKeys();
		return ipcount;
	}

	/**
	 * 定时任务处理newuv
	 */
	private int getNewUVByTime(String formDate, String toDate) {
		int newuvcount = 0;
		String mapFunction = "{cookieId: 0}";
		String reduceFunction = "function(doc, prev) {prev.cookieId+=1}";
		Criteria criteria = Criteria.where("access_date").gte(formDate).lt(toDate).and("visitCount").is(1);
		newuvcount = detailDao.group(criteria, CollectionNames.COOKIEID, mapFunction, reduceFunction, FlowisStatDetailDTO.class).getKeys();
		/*for (FlowisStatDetailDTO engine : result) {
			if (engine.getVisitCount().equals(1)) {
				newuvcount = Integer.valueOf(engine.getCookieId().split("\\.")[0]);
			}
		}*/
		return newuvcount;
	}
}
